/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  *Copyright(C),Tongji University
  * @file           : main.c
  * @brief          : Main program body
  * Author			    : ultralyj
  * Date		      	: 2020.10.27
  ******************************************************************************
  */

/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* peripheral extern declare */
extern TIM_HandleTypeDef htim4;
extern TIM_HandleTypeDef htim5;
extern IWDG_HandleTypeDef hiwdg;
extern uint16_t HMC5883LYaw;
extern uint16_t Key_Status;

/**
  * @brief  CFG the CLK,MX Devices
  * @retval none
  */
static void MX_Init(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* Configure the system clock */
  SystemClock_Config();
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_I2C1_Init();
  MX_SPI1_Init();
  MX_TIM3_Init();
  MX_USART2_UART_Init();
  MX_USART6_UART_Init();
  MX_USART1_UART_Init();
  MX_IWDG_Init();
  MX_TIM2_Init();
  MX_TIM4_Init();
  MX_TIM5_Init();
  MX_TIM9_Init();
}

/**
  * @brief  include all Initialization functions
  * @retval none
  */
void system_init(void)
{
  /* Initialize all the hardwares in the chip*/
	MX_Init();
  /* open two timers, TIM4 for BEEP, Tim5 for Motors*/
	HAL_TIM_Base_Start_IT(&htim4);
	HAL_TIM_Base_Start_IT(&htim5);
  /* Initialize the magnetometer(enable TIM2 at same time) */
  HAL_HMC5883L_Init();
  /* Initialize the ESP12F(and start receive data from UART2) */
  HAL_ESP12_Init();
  /* Blink the LED to tell you the system is running */
	HAL_GPIO_WritePin(GPIOA, LED_PA8_Pin, GPIO_PIN_SET);
	HAL_Delay(200);
	HAL_GPIO_WritePin(GPIOA, LED_PA8_Pin, GPIO_PIN_RESET);
  /* print the basic information to UART1 */
	printf("_____TCS_____\r\n");
	printf("Build Time:%s\r\n",__DATE__);
	printf("Copyright(C),Tongji University\r\n");
}

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  system_init();
  while (1)
  {
    /* feed the watchdog */
    HAL_IWDG_Refresh(&hiwdg);
    /* test code */
    HAL_Delay(1000);
    printf("[L883]%d[key]%d\r\n", HAL_HMC5883L_GetYawSafe(), HAL_GetKeyStatus());
  }
  /* USER CODE END 3 */
}
